<template>
  <div>
    <div class="hang">
      <!-- 导航栏 -->
      <p class="p" @click="orrr">
        <i class="el-icon-shopping-cart-full"></i>购物车(0)
      </p>
      <p @click="orr">我的收藏</p>
      <p @click="or">我的订单</p>
      <p @click="tuichu">退出登录</p>
      <p
        @click="dialogFormVisible1 = true;flag=2"
        v-if="!$store.state.users.userName"
      >
        注册
      </p>
      <p>|</p>
      <p
        @click="dialogFormVisible = true;flag=1"
        v-if="!$store.state.users.userName"
      >
        登录
      </p>
      <p>{{$store.state.users.userName}}</p>
    </div>
    <div class="s2"></div>
    <!-- shoppe图片 -->
    <div class="s3">
      <img src="http://101.132.181.9/img/logo.c8999c02.png" alt="" />
      <el-menu
        class="el-menu-demo"
        mode="horizontal"
        router
        active-text-color="#ffd04b"
        :default-active="activeIndex"
        @select="handleSelect"
      >
        <el-menu-item index="/">首页</el-menu-item>
        <el-menu-item index="/goods">全部商品</el-menu-item>
        <el-menu-item index="/about">关于我们</el-menu-item>
      </el-menu>
      <div class="s4">
        <el-input placeholder="请输入内容" class="input-with-select" />
        <el-button slot="append" icon="el-icon-search"></el-button>
      </div>
    </div>

    <router-view></router-view>

    <!-- 登录 -->
    <el-dialog title="登录" :visible.sync="dialogFormVisible">
      <el-form
        :model="ruleForm"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="ruleForm.userName"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <!-- 注册 -->
    <el-dialog title="注册" :visible.sync="dialogFormVisible1">
      <el-form
        :model="ruleForm1"
        ref="ruleForm1"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="ruleForm1.userName"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm1.password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="password">
          <el-input v-model="ruleForm1.password"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="submitForm1('ruleForm1')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { loginList, zclist } from "../api/user";
export default {
  data() {
    return {
      userId: this.$store.state.users.user_id,
      flag: 1,
      //默认路径
      activeIndex: "/",
      //注册模态框
      //登录模态框
      visible: false,
      dialogFormVisible: false,
      dialogFormVisible1: false,
      ruleForm: {
        userName: "",
        password: "",
      },
      ruleForm1: {
        userName: "",
        password: "",
      },
      // rules: {
      //   userName: [
      //     { required: true, message: "请输入用户名", trigger: "blur" },
      //     { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
      //   ],
      //   password: [
      //     { required: true, message: "请输入密码", trigger: "change" },
      //   ],
      // },
    };
  },
  computed: {
    title() {
      return this.flag == 1 ? "登录" : "注册";
    },
  },
  methods: {
    //退出登录
    tuichu() {
      this.$confirm("你确定要退出吗");
      this.$store.dispatch("cleanLogin");
    },
    orr() {
      if (this.userId == undefined) {
        this.dialogFormVisible = true;
      } else {
        this.$router.push("/collect");
      }
    },
    orrr() {
      if (this.userId == undefined) {
        this.dialogFormVisible = true;
      } else {
        this.$router.push("/shoppingCart");
      }
    },
    handleSelect(key, keyPath) {
      console.log(key, keyPath);
    },
    or() {
      // console.log(this.userId);
      if (this.userId == undefined) {
        this.dialogFormVisible = true;
      } else {
        this.$router.push("/order");
      }
    },

    // 注册
    submitForm1(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // alert('submit!');
          try {
            let res = await zclist(this.ruleForm1);
            console.log("res", res);
            if (res.code == "001") {
              this.dialogFormVisible1 = false;
              // localStorage.setItem("token",JSON.stringify(this.ruleForm))
            }
          } catch (error) {
            console.log(error);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // alert('submit!');
          try {
            let res = await loginList(this.ruleForm);
            console.log("res", res);
            if (res.code == "001") {
              this.dialogFormVisible = false;
              this.text1 = "欢迎" + this.ruleForm.userName;
              // localStorage.setItem("token",JSON.stringify(this.ruleForm))
              this.$store.commit("setuser", res.user);
              this.$notify.success({
                title: "Info",
                message: "欢迎登录成功",
                showClose: false,
              });
              this.flag = 1;
            }
          } catch (error) {
            console.log(error);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
}
//数据
.hang {
  width: 100%;
  height: 40px;
  background-color: #3d3d3d;
  line-height: 40px;
  color: #b0b0b0;
  p {
    float: right;
    margin-left: 40px;
  }
  .p {
    margin-right: 50px;
    width: 100px;
    height: 40px;
  }
}
.p:hover {
  background-color: white;
  color: red;
}
p:hover {
  color: white;
}
.s2 {
  width: 100%;
  height: 30px;
  background-color: white;
}
.s3 {
  width: 90%;
  margin: auto;
  height: 61px;
  // background-color: yellow;
  display: flex;
  justify-content: space-around;
  padding-left: 50px;
  position: relative;
  border-bottom: 1px solid #e6e6e6;
  img {
    width: 189px;
    height: 59px;
    // position: absolute;
    // margin-left: 100px;
  }
  .el-menu {
    line-height: 59px;
  }

  .el-input {
    width: 200px;
    line-height: 61px;
  }
}

.s5 {
  width: 93%;
  height: 460px;
  background-color: red;
  margin: auto;
  margin-top: 5px;
}
</style>